<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use ModelTree, AdminBuilder;
    protected $fillable = ['pid', 'type', 'name', 'nickname', 'flag', 'image', 'description', 'weigh', 'status'];

    /**
     * 通过pid获取分类列表
     * @param $pid
     * @param string $type
     * @return mixed
     */
    public static function getCategoryListByPid($parent_id, $type = 'topic'){
        if ($parent_id != 0 && empty($parent_id)) return null;

        $list = self::where([
                'parent_id' => $parent_id,
                'status' => 1,
                'type' => $type
            ])
            ->select(['id', 'parent_id', 'type', 'title', 'nickname', 'flag', 'image'])
            ->orderBy('order', 'asc')
            ->get();

        return $list;
    }
}
